package com.leps.nq.tx.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leps.nq.bi.vo.SimpleEntry;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.tx.entity.PumpEffectivePoint;
import com.leps.nq.tx.entity.ReadHistory;
import com.leps.nq.tx.mapper.PumpEffectivePointMapper;
import com.leps.nq.tx.mapper.ReadHistoryMapper;
import com.leps.nq.tx.service.IReadHistoryService;
import com.leps.nq.tx.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hex
 * @since 2022-08-10
 */
@Service
public class ReadHistoryServiceImpl extends ServiceImpl<ReadHistoryMapper, ReadHistory> implements IReadHistoryService {
    @Autowired
    PumpEffectivePointMapper pumpEffectivePointMapper;
    @Autowired
    ReadHistoryMapper readHistoryMapper;

    @Override
    public PumpDataVo getLastPumpStateByPumpId(int pumpId) {
        List<ReadHistoryVo> history = this.getBaseMapper().selectLastReadHistoryByPumpId(pumpId);
        if (ObjectUtil.isEmpty(history)) return null;
        List<Double> dataList = history.stream().map(h -> h.getDataValue()).collect(Collectors.toList());
        PumpPointVo pumpPointVo = this.pumpEffectivePointMapper.selectPumpPointVoByPumpId(pumpId);
        PumpDataVo dataVo = new PumpDataVo();
        dataVo.setPumpId(pumpId);
        dataVo.setPumpEffectivePoints(pumpPointVo.getEffectivePoints());
        dataVo.setRunning(Convert.toInt(dataList.get(0)));
        dataVo.setOpenSetupMinutes(Convert.toInt( dataList.get(1)));
        dataVo.setCloseSetupMinutes(Convert.toInt(dataList.get(2)));
        dataVo.setBroken(Convert.toInt(dataList.get(3)));
        dataVo.setOpenRealMinutes(Convert.toInt(dataList.get(4)));
        dataVo.setCloseRealMinutes(Convert.toInt(dataList.get(5)));
        dataVo.setLiquidLevel(Convert.toInt(dataList.get(8)));
        dataVo.setPressure(Convert.toInt(dataList.get(9)));
        // if there is a record of liquid  add manuly later, replace it.
        ReadHistory lastLiquid = this.readHistoryMapper.selectLastLiquidByPumpId(pumpId);
        if (lastLiquid != null && lastLiquid.getSerialNumber() == null) {
            dataVo.setLiquidLevel(Convert.toInt(lastLiquid.getDataValue()));
        }
        return dataVo;
    }

    @Override
    public long getLastSerialNumberByPlcId(int plcId) {
        Long serialNumber = this.getBaseMapper().selectLastSerialNumberByPlcId(plcId);
        if (serialNumber == null) {
            serialNumber = 0L;
        }
        return serialNumber;
    }

    @Override
    public List<SimpleEntry<Double>> getRecentLiquidLevels(int pumpId) {
        List<SimpleEntry<Double>> list = this.getBaseMapper().selectRecentLiquidLevels(pumpId);
        if(list != null)
            Collections.reverse(list);
        return list;
    }

    @Override
    public void addLiquid(CreateLiquidVo vo) {
        final int pumpId = vo.getPumpId();
        // 查实时液位点位
        PumpPointVo pumpPointVo = this.pumpEffectivePointMapper.selectPumpPointVoByPumpId(pumpId);
        List<PumpEffectivePoint> effectivePoints = pumpPointVo.getEffectivePoints();
        PumpEffectivePoint point = effectivePoints.stream().filter(p -> "实时液位".equalsIgnoreCase(p.getSpec())).findFirst().get();
        final int pepId = point.getPepId();
        final Integer ratio = point.getRatio();
        double dataValue = vo.getLiquid();
        // 倍数处理
        if(ratio != null && ratio != 0) {
            dataValue = dataValue * ratio;
        }
        // 生成手动液位记录
        ReadHistory history = new ReadHistory();
        history.setPlcId(pumpPointVo.getPlcId());
        history.setPumpId(pumpId);
        history.setPointId(pepId);
        history.setDataValue(dataValue);
        history.setReadTime(new Date());
        this.readHistoryMapper.insert(history);
    }

    @Override
    public PageVo<ReadHistory> pageLiquidByQueryModel(LiquidQueryModel queryModel) {
        Page<ReadHistory> page = Page.of(queryModel.getPageNum(), queryModel.getPageSize());
        page = this.readHistoryMapper.pageLiquidByQueryModel(page, queryModel);
        return PageVo.of(page);
    }
}
